package com.collect.zambia.zhiCallLogWorker;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;

import java.util.concurrent.TimeUnit;

public class ZHITokenManager {
    public static void updateToken(Context context, String newToken) {
        // 更新 SharedPreferences 中的 Token
        SharedPreferences sharedPreferences = context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("auth_token", newToken);
        editor.apply();

        // 重新调度上传任务
        PeriodicWorkRequest uploadWorkRequest = new PeriodicWorkRequest.Builder(
                ZHICallLogUploadWorker.class,
                10, TimeUnit.MINUTES
        ).build();

        WorkManager.getInstance(context).enqueueUniquePeriodicWork(
                "uploadWork",
                ExistingPeriodicWorkPolicy.REPLACE, // 替换现有任务
                uploadWorkRequest
        );
    }
}
